#include "instructions/references/new.hpp"
#include "rtda/frame.h"
#include "rtda/heap/cp_classref.h"
#include <stdexcept>

namespace jvm {
namespace instructions {

void New::Execute(Frame& frame) {
    auto cp = frame.getMethod()->getClass()->getConstantPool();
    auto classRef = std::dynamic_pointer_cast<ClassRef>(cp->getConstant(index_));
    auto clazz = classRef->resolvedClass();
    // TODO: init class

    if (clazz->isInterface() || clazz->isAbstract()) {
        throw std::runtime_error("java.lang.InstantiationError");
    }

    auto ref = clazz->newObject();
    frame.getOperandStack()->pushRef(ref);
}

} // namespace instructions
} // namespace jvm